﻿using System.Collections.Generic;
using System.Linq;
using com.eforceglobal.crux.DTOs;
using com.eforceglobal.crux.BusinessObjects;
using com.eforceglobal.crux.BusinessObjects.DatabaseQueries;

namespace com.eforceglobal.crux.BusinessFacades
{
    public class UserDetailsFacade
    {
        Queries queries = new Queries();

        public void SyncronizeRoleDetailsForUser (UserDetailsInformation userdetailsInformation, int companyid, bool ImmediateSubmit)
        {
            IEnumerable<UserRole> dbUserRoles = queries.GetUserRolesForUser(userdetailsInformation.ConcernedUser.UserID, companyid);
           
            //Remove Role Assignment
            foreach (UserRole dbUserRole in dbUserRoles)
            {
                if (userdetailsInformation.Roles.SingleOrDefault(p => p.RoleID == dbUserRole.RoleID) == null)
                    queries.RemoveUserFromRole(dbUserRole , false);
            }

            // Add Role Assignment
            foreach (RoleInformation role in userdetailsInformation.Roles)
            {
                if (dbUserRoles.SingleOrDefault(p => p.RoleID == role.RoleID) == null)
                {
                    UserRole dbUserRole = new UserRole();
                    dbUserRole.UserID = userdetailsInformation.ConcernedUser.UserID;
                    dbUserRole.RoleID = role.RoleID;
                    queries.AssignRolesToUser(dbUserRole, false);
                }
            }
            if(ImmediateSubmit) queries.SubmitChanges();
        }


        public void SyncronizeDepartmentDetailsForUser(UserDetailsInformation userdetailsInformation, int companyid, bool ImmediateSubmit)
        {
            IEnumerable<UserDepartment> dbUserDepartments = queries.GetUserDepartmentsForUser(userdetailsInformation.ConcernedUser.UserID, companyid);
          
            // Removal Of Department Assignment
            foreach (UserDepartment dbUserDepartment in dbUserDepartments)
            {
                if (userdetailsInformation.Departments
                    .SingleOrDefault(p => p.DepartmentID == dbUserDepartment.DepartmentID) == null)
                    queries.RemoveUserFromDepartment(dbUserDepartment, false);
            }

            // Add Department Assignment
            foreach (DepartmentInformation department in userdetailsInformation.Departments)
            {
                if (dbUserDepartments.SingleOrDefault(p => p.DepartmentID == department.DepartmentID) == null)
                {
                    UserDepartment dbUserDepartment = new UserDepartment();
                    dbUserDepartment.UserID = userdetailsInformation.ConcernedUser.UserID;
                    dbUserDepartment.DepartmentID = department.DepartmentID;
                    queries.AssignDepartmentToUser(dbUserDepartment, false);
                }
            }

            if(ImmediateSubmit) queries.SubmitChanges();
        }

        /// <summary>
        /// For All Deferred Submits on A Facade Instance.
        /// </summary>
        public void SubmitFacadeQueries()
        {
            queries.SubmitChanges();
        }


        public void SyncronizeCompaniesToRepresentForUser(UserDetailsInformation userDetailsInformation, bool ImmediateSubmit)
        {
            IEnumerable<CompanyRepresentative> dbCompanyRepresentatives = queries.GetCompanyRepresentativeForUser(userDetailsInformation.ConcernedUser.UserID);

            //Remove Role Assignment
            foreach (CompanyRepresentative dbCompanyRepresentative in dbCompanyRepresentatives)
            {
                if (userDetailsInformation.CompaniesToRepresent.SingleOrDefault(p => p.CompanyID == dbCompanyRepresentative.CompanyID) == null)
                    queries.RemoveCompanyRepresentative(dbCompanyRepresentative, false);
            }

            // Add Role Assignment
            foreach (CompanyInformation company in userDetailsInformation.CompaniesToRepresent)
            {
                if (dbCompanyRepresentatives.SingleOrDefault(p => p.CompanyID == company.CompanyID) == null)
                {
                    CompanyRepresentative dbCompanyRepresentative = new CompanyRepresentative();
                    dbCompanyRepresentative.UserID = userDetailsInformation.ConcernedUser.UserID;
                    dbCompanyRepresentative.CompanyID = company.CompanyID;
                    queries.AssignRepresentativeToCompany(dbCompanyRepresentative, false);
                }
            }
            if (ImmediateSubmit) queries.SubmitChanges();

        }



        public void SyncronizeLocationsToRepresentForUser(UserDetailsInformation userDetailsInformation, bool ImmediateSubmit)
        {
            IEnumerable<LocationRepresentative> dbLocationRepresentatives = queries.GetLocationRepresentativeForUser(userDetailsInformation.ConcernedUser.UserID);

            //Remove Role Assignment
            foreach (LocationRepresentative dbLocationRepresentative in dbLocationRepresentatives)
            {
                if (userDetailsInformation.LocationsToRepresent.SingleOrDefault(p => p.LocationID == dbLocationRepresentative.LocationID) == null)
                    queries.RemoveLocationRepresentative(dbLocationRepresentative, false);
            }

            // Add Role Assignment
            foreach (LocationInformation location in userDetailsInformation.LocationsToRepresent)
            {
                if (dbLocationRepresentatives.SingleOrDefault(p => p.LocationID == location.LocationID) == null)
                {
                    LocationRepresentative dbLocationRepresentative = new LocationRepresentative();
                    dbLocationRepresentative.UserID = userDetailsInformation.ConcernedUser.UserID;
                    dbLocationRepresentative.LocationID = location.LocationID;
                    queries.AssignRepresentativeToLocation(dbLocationRepresentative, false);
                }
            }
            if (ImmediateSubmit) queries.SubmitChanges();

        }
    }
}
